package com.goscom.app.deeset;

import java.util.ArrayList;
import com.goscom.app.database.DBAdapter;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class ContractedHours extends Activity {

	private String strMerId = "";
	private String strSearch = "";
	private DBAdapter mDB;
	private EditText mEdtStoreName;
	private ListView mLstContracted, mLstNonContracted;
	private ArrayAdapter<String> mAdapterCont, mAdapterNonCont;
	private ArrayList<String> arrlstContracted, arrlstNonContracted,
			arrlstContractedId, arrlstNonContractedId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contractedhours);

		mEdtStoreName = (EditText) findViewById(R.id.edtContractedStoName);
		mLstContracted = (ListView) findViewById(R.id.lstContracted);
		mLstNonContracted = (ListView) findViewById(R.id.lstContractedNon);

		mDB = new DBAdapter(ContractedHours.this);
		mDB.open();

		getMerId();
		
		mLstContracted.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(ContractedHours.this, AddContracted.class);
				intent.putExtra("merid", strMerId);
				intent.putExtra("activity", "contracted");
				intent.putExtra("search", strSearch);
				intent.putExtra("contractedid", arrlstContractedId.get(arg2));
				startActivity(intent);
			}
		});
		
		mLstNonContracted.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(ContractedHours.this, AddContracted.class);
				intent.putExtra("merid", strMerId);
				intent.putExtra("activity", "noncontracted");
				intent.putExtra("search", strSearch);
				intent.putExtra("contractedid", arrlstNonContractedId.get(arg2));
				startActivity(intent);
			}
		});
	}

	public void getMerId() {
		try {
			strMerId = getIntent().getStringExtra("merid");
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (strMerId != null) {
			loadContractedHours();
			loadNonContractedHours();
		}
	}

	public void loadContractedHours() {
		ArrayList<ContentValues> arrlst = new ArrayList<ContentValues>();
		arrlst = mDB.queryListByMerContracted("CONTRACTEDHOURS", strMerId,
				"contracted");
		if (arrlst.size() != 0) {
			arrlstContracted = new ArrayList<String>();
			arrlstContractedId = new ArrayList<String>();
			for (int i = 0; i < arrlst.size(); i++) {
				ContentValues content = new ContentValues();
				content = arrlst.get(i);
				arrlstContracted.add(content.getAsString("storel") + " - "
						+ content.getAsString("jobskill"));
				arrlstContractedId.add(content.getAsString("contractedid"));
			}
			mAdapterCont = new ArrayAdapter<String>(ContractedHours.this,
					android.R.layout.simple_list_item_1, arrlstContracted);
			mLstContracted.setAdapter(mAdapterCont);
		}
	}
	
	public void loadNonContractedHours() {
		ArrayList<ContentValues> arrlst = new ArrayList<ContentValues>();
		arrlst = mDB.queryListByMerContracted("CONTRACTEDHOURS", strMerId,
				"noncontracted");
		if (arrlst.size() != 0) {
			arrlstNonContracted = new ArrayList<String>();
			arrlstNonContractedId = new ArrayList<String>();
			for (int i = 0; i < arrlst.size(); i++) {
				ContentValues content = new ContentValues();
				content = arrlst.get(i);
				arrlstNonContracted.add(content.getAsString("storel") + " - "
						+ content.getAsString("jobskill"));
				arrlstNonContractedId.add(content.getAsString("contractedid"));
			}
			mAdapterNonCont = new ArrayAdapter<String>(ContractedHours.this,
					android.R.layout.simple_list_item_1, arrlstNonContracted);
			mLstNonContracted.setAdapter(mAdapterNonCont);
		}
	}
	
	public void getSimilarStore(View v) {
		strSearch = mEdtStoreName.getText().toString();
		Toast.makeText(ContractedHours.this, "Search Store And Retailer with:" + strSearch,
				Toast.LENGTH_LONG).show();
		Log.i("search", strSearch);
	}

	public void addStoreContracted(View v) {
		Intent intent = new Intent(ContractedHours.this, AddContracted.class);
		intent.putExtra("merid", strMerId);
		intent.putExtra("activity", "contracted");
		intent.putExtra("search", strSearch);
		startActivity(intent);
	}

	public void addStoreNonContracted(View v) {
		Intent intent = new Intent(ContractedHours.this, AddContracted.class);
		intent.putExtra("merid", strMerId);
		intent.putExtra("activity", "noncontracted");
		intent.putExtra("search", strSearch);
		startActivity(intent);
	}

	public void Logout(View v) {
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(ContractedHours.this, Login.class));
	}

	public void goPrevious(View v) {
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(ContractedHours.this, OfferLetter.class)
				.putExtra("merid", strMerId));
	}

	public void goNext(View v) {
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(ContractedHours.this, MedicalHistory.class)
				.putExtra("merid", strMerId));
	}

	public void Save(View v) {
		Toast.makeText(ContractedHours.this, "Save Successful!",
				Toast.LENGTH_LONG).show();
	}

	@Override
	protected void onRestart() {
		mDB.open();
		super.onRestart();
	}

	@Override
	protected void onDestroy() {
		if (mDB != null) {
			mDB.close();
		}
		super.onDestroy();
	}
}
